//
//  MYGBottomSheetView.swift
//  MYGrow
//
//  Created by 林矗 on 2024/3/16.
//

import UIKit
let kMinAppearanceViewHeight = 60.0
class MYGBottomSheetView: MYGCommonMaskView {
    // MARK: - Property
    var appearanceView: UIView {
        get {
            return innerAppearnceView
        }
    }
    
    var isShown: Bool = false
    var title: String = "" {
        didSet {
            titleLabel.text = title
        }
    }
    var customView: UIView?
    var touchWildToHide = true
    
    private var appearanceViewMaxHeight: CGFloat {
        return self.frame.size.height * 0.8375
    }
    // MARK: - init
    init(with customView: UIView) {
        self.customView = customView
        super.init(frame: .zero)
        configUI()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        self.maskColor = .rgba(0, 0, 0, 0.6)
        addSubview(innerAppearnceView)
        innerAppearnceView.snp.makeConstraints { make in
            make.height.greaterThanOrEqualTo(kMinAppearanceViewHeight)
            make.height.lessThanOrEqualTo(appearanceViewMaxHeight)
            make.left.right.equalToSuperview()
            make.top.equalTo(self.snp.bottom)
        }
        
        innerAppearnceView.addSubview(topView)
        topView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(60)
        }
        
        topView.addSubview(titleLabel)
        topView.addSubview(closeButton)
        titleLabel.snp.makeConstraints { make in
            make.centerX.centerY.equalToSuperview()
        }
        
        closeButton.snp.makeConstraints { make in
            make.top.equalTo(14)
            make.right.equalTo(-12)
            make.size.equalTo(CGSize(width: 13, height: 13))
        }
        
        innerAppearnceView.addSubview(customContainerView)
        customContainerView.snp.makeConstraints { make in
            make.height.greaterThanOrEqualTo(0)
            make.left.right.equalToSuperview()
            make.top.equalTo(topView.snp.bottom)
            make.bottom.equalToSuperview()
        }
        
        guard let customView = customView else { return }
        customView.translatesAutoresizingMaskIntoConstraints = false
        customContainerView.addSubview(customView)
        customView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        invalidateIntrinsicContentSize()
    }
    
    
    override func maskViewTapped() {
        hide(animated: true)
    }
    
    
    
    // MARK: - Public funcs
    public func show(animated: Bool) {
        show(in: nil, animated: animated)
    }
    
    public func hide(animated: Bool) {
        if !isShown { return }
        layoutIfNeeded()
        let duration = animated ? 0.25 : 0
        UIView.animate(withDuration: duration) {
            self.innerAppearnceView.snp.remakeConstraints { make in
                make.height.greaterThanOrEqualTo(kMinAppearanceViewHeight)
                make.height.lessThanOrEqualTo(self.appearanceViewMaxHeight)
                make.left.right.equalToSuperview()
                make.top.equalTo(self.snp.bottom)
            }
            self.layoutIfNeeded()
        } completion: { finished in
            self.removeFromSuperview()
        }
        isShown = false
    }
    
    public func show(in view: UIView?, animated: Bool) {
        let inView = view ?? UIApplication.shared.keyWindow
        inView?.addSubview(self)
        self.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        layoutIfNeeded()
        let duration = animated ? 0.25 : 0
      
        UIView.animate(withDuration: duration) {
            self.innerAppearnceView.snp.remakeConstraints { make in
                make.height.greaterThanOrEqualTo(kMinAppearanceViewHeight)
                make.height.lessThanOrEqualTo(self.appearanceViewMaxHeight)
                make.left.right.equalToSuperview()
                make.bottom.equalTo(self.snp.bottom)
            }
            self.layoutIfNeeded()
        } completion: { finished in
            
        }
        isShown = true
    }
    
    // MARK: - lazy load
    private lazy var innerAppearnceView: UIView =  {
       let appearnceView = UIView()
        appearnceView.translatesAutoresizingMaskIntoConstraints = false
        appearnceView.backgroundColor = .rgba(17, 17, 17)
        return appearnceView
    }()
    
    private lazy var customContainerView: UIView = {
       let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .clear
        return view
    }()
    
    private lazy var topView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = .black
        label.font = .medium(17)
        return label
    }()
    
    private lazy var closeButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "Common/close"), for: .normal)
        button.addTarget(self, action: #selector(tapGestrueTriggered), for: .touchUpInside)
        return button
    }()
}
